package com.xtourms.client.ui.util;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.xtourms.client.Context;
import com.xtourms.shared.field.LocationField;

public class LocationWidget extends Composite {

	private static LocationWidgetUiBinder uiBinder = GWT
			.create(LocationWidgetUiBinder.class);

	interface LocationWidgetUiBinder extends UiBinder<Widget, LocationWidget> {
	}

	@UiField ListBox province;
	@UiField ListBox city;
	
	public LocationWidget() {
		initWidget(uiBinder.createAndBindUi(this));
		for(int i = 0; i < Context.ins.location.size(); i++){
			JSONObject p = (JSONObject)Context.ins.location.get(i);
			String pName = ((JSONString)p.get(LocationField.PROVINCE_NAME)).stringValue();
			province.addItem(pName);
			if(0 == i){
				fillCity(p);
			}
		}
		
		province.addChangeHandler(new ChangeHandler() {
			
			@Override
			public void onChange(ChangeEvent event) {
				int index = province.getSelectedIndex();
				city.clear();
				JSONObject p = (JSONObject)Context.ins.location.get(index);
				fillCity(p);
			}
		});
	}

	private void fillCity(JSONObject p) {
		JSONArray cs = (JSONArray) p.get(LocationField.CITYS);
		for(int j = 0; j < cs.size(); j++){
			JSONObject c = (JSONObject) cs.get(j);
			String cName = ((JSONString)c.get(LocationField.CITY_NAME)).stringValue();
			city.addItem(cName);
		}
	}

	public int getSelectedCityId(){
		int pI = province.getSelectedIndex();
		int cI = city.getSelectedIndex();
		JSONObject p = (JSONObject)Context.ins.location.get(pI);
		JSONArray cs = (JSONArray)p.get(LocationField.CITYS);
		JSONObject c = (JSONObject)cs.get(cI);
		int ret = Integer.parseInt(c.get(LocationField.CITY_ID).toString());
		return ret;
	}
	
	public void reset(){
		province.setSelectedIndex(0);
		city.clear();
		JSONObject p = (JSONObject)Context.ins.location.get(0);
		fillCity(p);
	}
	
	public void setLocation(String pN, String cN){
		JSONArray l = Context.ins.location;
		for(int i = 0; i < l.size(); i++){
			JSONObject p = (JSONObject)l.get(i);
			String pName = ((JSONString)p.get(LocationField.PROVINCE_NAME)).stringValue();
			if(pName.equals(pN)){
				province.setSelectedIndex(i);
				JSONArray cs = (JSONArray) p.get(LocationField.CITYS);
				for(int j = 0; j < cs.size(); j++){
					JSONObject c = (JSONObject) cs.get(j);
					String cName = ((JSONString)c.get(LocationField.CITY_NAME)).stringValue();
					if(cName.equals(cN)){
						city.setSelectedIndex(j);
					}
				}
			}
		}
	}
}
